Load(1);
ajax(api.menu.list,{is_tree:1},'post').done(function(res) {
	console.log(res)
	var  level= `<option value='0'>顶级</option>`;
	var tree=[]
	if(res.code == 1){
		for(var i1=0;i1<res.data.length;i1++){
			res.data[i1].name2=res.data[i1].name
			tree.push(res.data[i1])
			for(var i2=0;i2<res.data[i1].children.length;i2++){
				res.data[i1].children[i2].name2='├'+res.data[i1].children[i2].name
				tree.push(res.data[i1].children[i2])
				for(var i3=0;i3<res.data[i1].children[i2].children.length;i3++){
					res.data[i1].children[i2].children[i3].name2='├├'+res.data[i1].children[i2].children[i3].name
					tree.push(res.data[i1].children[i2].children[i3])
				}
			}
		}
		for(var i =0; i<tree.length;i++){
			level+=`<option value='${tree[i].id}'>${tree[i].name2}</option>`
		}
		$('#pid').html(level)
	}else{
		Err('数据请求失败')
	}
	form.render('select');
}).fail(function(err) {
	Err('请求失败')
}); 

{if $Think.get.id}
Load();
	 ajax(api.menu.list,{
		where_json:JSON.stringify({
			id:['in',"{$Think.get.id}"]
		}),is_tree:0
	},'post').done(function(res) {
		Load(1);
		console.log(res)
		for(var key in res.data[0]){
			$('input[name='+key+']').val(res.data[0][key])
			if(key =='pid'||key =='mode'){
				$('select[name='+key+']').val(res.data[0][key])
			}
		}
		form.render('select')
	}).fail(function(err) {
		Err('请求失败')
	}); 
	  {else/}
	  {/if}
form.on('submit(ss1)', function(data){
	
	   //当前容器的全部表单字段，名值对形式：{name: value}
	  data.field.module_type=0
	  console.log(data.field)
	  {if $Think.get.id}
	  data.field.id ='{$Think.get.id}'
	  ajax(api.menu.edit,data.field,'post').done(function(res) {
		console.log(res)
		if(res.code==1){
			Msg('编辑成功')
			// window.location.href='{:url("/html/system/menu")}'
		}else{
			Err(res.msg)
		}
		}).fail(function(err) {
			Err('请求失败')
		});
	  {else/}
	 	ajax(api.menu.add,data.field,'post').done(function(res) {
				console.log(res)
				if(res.code==1){
					Msg('添加成功')
					// window.location.href='{:url("/html/system/menu")}'
				}else{
					Err(res.msg)
				}
		}).fail(function(err) {
			Err('请求失败')
		});
		{/if}
	  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
	});